import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.*;
import java.util.List;
import java.util.Timer;

public class ShootPannel extends JPanel implements MouseMotionListener {

    Image bgImg = new ImageIcon("src/plane/background.jpg").getImage();
    int bgX=0,bgY=0;
    Hero hero = new Hero(200,600,99,1,0);

    int heroImgFlag=1;

    List<Bullet> bullets = new ArrayList<Bullet>();

    List<Enemy> enemies = new ArrayList<Enemy>();

    long time=0;


    public ShootPannel(){
        this.addMouseMotionListener(this);
    }

    public void bgMove(Graphics g){
        bgY+=5;
        g.drawImage(bgImg,bgX,bgY,null);
        g.drawImage(bgImg,bgX,bgY-ShootFrame.HEIGHT,null);
        if(bgY>ShootFrame.HEIGHT){
            bgY=0;
        }
    }

    public void paintHero(Graphics g){
        heroImgFlag++;
        if(heroImgFlag==10){
            heroImgFlag=1;
        }
        g.drawImage(new ImageIcon("src/plane/ws0"+heroImgFlag+".png").getImage(),hero.x,hero.y,null);
    }

    public void paintText(Graphics g){
        g.setColor(Color.YELLOW);
        Font font = new Font("微软雅黑",Font.BOLD,35);
        g.setFont(font);
        g.drawString("当前分数："+hero.score,0,ShootFrame.HEIGHT-50);
    }

    public void paintBullet(Graphics g){
        for(int i=0;i<bullets.size();i++){
            bullets.get(i).move();
            if(bullets.get(i).alive==1){
                g.drawImage(new ImageIcon("src/plane/bullets.png").getImage(),bullets.get(i).x,bullets.get(i).y,null);
            }
            if(bullets.get(i).y<0 || bullets.get(i).alive==0){
                bullets.remove(i);
            }
        }
    }

    public void paintEnemy(Graphics g){
        Random random = new Random();
        if(time%1000==0){
            enemies.add(new Enemy(random.nextInt(ShootFrame.WIDTH-50),0));
        }

        for(int i=0;i<enemies.size();i++){
            for(int j=0;j<bullets.size();j++){
                if((bullets.get(j).x>enemies.get(i).x) && (bullets.get(j).x<enemies.get(i).x+50) && (bullets.get(j).y<enemies.get(i).y+50) && (bullets.get(j).y>enemies.get(i).y)){
                    // 被命中
                    bullets.get(j).alive=0;
                    enemies.get(i).alive=0;
                    // 该单位与子弹同归于尽
                    hero.score++;
                }
            }
            if((enemies.get(i).y>ShootFrame.HEIGHT) || (enemies.get(i).alive==0)){
                enemies.remove(i);
            }
        }

        for(int i=0;i<enemies.size();i++){
            enemies.get(i).move();
            g.drawImage(new ImageIcon("src/plane/flys.png").getImage(),enemies.get(i).x,enemies.get(i).y,null);


        }

    };

    public void paint(Graphics g){
        super.paint(g);
        this.bgMove(g);
        this.paintHero(g);
        this.paintText(g);
        this.paintBullet(g);
        this.paintEnemy(g);
    }

    public void action(){
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                bullets.add(hero.shoot(hero.x+50,hero.y));
                repaint();
                time+=50;
            }
        },0,50);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        hero.x=e.getX()-50;
        hero.y=e.getY()-50;
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        hero.x=e.getX()-50;
        hero.y=e.getY()-50;
    }
}
